<?php
/**
 * Created by PhpStorm.
 * User: Emmett
 * Date: 2016/8/30
 * Time: 17:30
 */

namespace Business\Home;

use Common\Common\GlobalParam;

class ManagerHomeStrategy extends HomeStrategyBase
{
    public function index()
    {
        parent::index();
        $user_id = $this->mvc_controller->current_user['id'];
        $crm_custom = M('crm_custom');

        $this->follow_count($user_id, $crm_custom);
        $this->signed_count($user_id, $crm_custom);
        $this->add_count($user_id, $crm_custom);

        $crm_activity = M('crm_activity');
        $this->phone_count($user_id, $crm_activity);

        $this->depart_count($user_id, $crm_custom, $crm_activity);
    }


    protected function follow_count($user_id, $crm_custom)
    {
        //我跟进的客户数量
        $condition = array();
        $condition['leader_user_id'] = $user_id;
        $condition['state'] = GlobalParam::$Custom_State_Distributed;

        $follow_count = $crm_custom->where($condition)->count();
        $this->mvc_controller->assign('follow_count', $follow_count);
    }

    protected function signed_count($user_id, $crm_custom)
    {
        //我签约的客户数量
        $condition = array();
        $condition['sign_user_id'] = $user_id;
        $condition['state'] = GlobalParam::$Custom_State_Signed;

        $signed_count = $crm_custom->where($condition)->count();
        $this->mvc_controller->assign('signed_count', $signed_count);
    }

    protected function add_count($user_id, $crm_custom)
    {
        //我新增的客户数量
        $condition = array();
        $condition['add_user_id'] = $user_id;
        $condition['state'] = array('egt', GlobalParam::$Custom_State_Undistributed);

        $add_count = $crm_custom->where($condition)->count();
        $this->mvc_controller->assign('add_count', $add_count);
    }

    protected function phone_count($user_id, $crm_activity)
    {
        //我的电话量
        $condition = array();
        $condition['add_user_id'] = $user_id;
        $condition['type'] = array('eq', 1);
        $condition['state'] = array('eq', 0);

        $phone_count = $crm_activity->where($condition)->count();
        $this->mvc_controller->assign('phone_count', $phone_count);
    }

    protected function depart_count($user_id, $crm_custom, $crm_activity)
    {
        $user_ids = fc_get_employee_ids_by_uid($user_id);

        //跟进的客户数量
        $condition = array();
        $condition['leader_user_id'] = array('in', $user_ids);
        $condition['state'] = GlobalParam::$Custom_State_Distributed;

        $follow_count = $crm_custom->where($condition)->field('leader_user_id as uid,count(leader_user_id) as count_value')->group('leader_user_id')->select();

        $condition = array();
        $condition['sign_user_id'] = array('in', $user_ids);
        $condition['state'] = GlobalParam::$Custom_State_Signed;

        $signed_count = $crm_custom->where($condition)->field('sign_user_id as uid,count(sign_user_id) as count_value')->group('sign_user_id')->select();

        $condition = array();
        $condition['add_user_id'] = array('in', $user_ids);
        $condition['state'] = array('egt', GlobalParam::$Custom_State_Undistributed);

        $add_count = $crm_custom->where($condition)->field('add_user_id as uid,count(add_user_id) as count_value')->group('add_user_id')->select();

        $condition = array();
        $condition['add_user_id'] = array('in', $user_ids);
        $condition['type'] = array('eq', 1);
        $condition['state'] = array('eq', 0);

        $phone_count = $crm_activity->where($condition)->field('add_user_id as uid,count(add_user_id) as count_value')->group('add_user_id')->select();

        $sum_follow_count=0;
        $sum_signed_count=0;
        $sum_add_count=0;
        $sum_phone_count=0;
        $depart_count = [];
        foreach ($user_ids as $user_id) {
            $row = [];
            $row['uid'] = $user_id;
            $ufc = searchArray($follow_count, 'uid', $user_id);
            if (!empty($ufc)) {
                $row['follow_count'] = $ufc['count_value'];
                $sum_follow_count=$sum_follow_count+$ufc['count_value'];
            } else {
                $row['follow_count'] = 0;
            }
            $ufc = searchArray($signed_count, 'uid', $user_id);
            if (!empty($ufc)) {
                $row['signed_count'] = $ufc['count_value'];
                $sum_signed_count=$sum_signed_count+$ufc['count_value'];
            } else {
                $row['signed_count'] = 0;
            }
            $ufc = searchArray($add_count, 'uid', $user_id);
            if (!empty($ufc)) {
                $row['add_count'] = $ufc['count_value'];
                $sum_add_count=$sum_add_count+$ufc['count_value'];
            } else {
                $row['add_count'] = 0;
            }
            $ufc = searchArray($phone_count, 'uid', $user_id);
            if (!empty($ufc)) {
                $row['phone_count'] = $ufc['count_value'];
                $sum_phone_count=$sum_phone_count+$ufc['count_value'];
            } else {
                $row['phone_count'] = 0;
            }
            $depart_count[] = $row;
        }

        $this->mvc_controller->assign('depart_count', $depart_count);

        $sum_depart_count=array("sum_follow_count" => $sum_follow_count, "sum_signed_count" => $sum_signed_count,
                                "sum_add_count" => $sum_add_count,"sum_phone_count" => $sum_phone_count);
        $this->mvc_controller->assign('sum_depart_count', $sum_depart_count);
    }
}